<template>
  <div style="margin-top: 50px; width:500px">
    <el-form ref="editForm" label-width="200px" :model="editForm" :rules="editRule">

      <el-form-item label="旧密码" prop="oldpassword">
        <el-input v-model="editForm.oldpassword" type="password" />
      </el-form-item>
      <el-form-item label="新密码" prop="newpassword">
        <el-input v-model="editForm.newpassword" type="password" />
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmpassword">
        <el-input v-model="editForm.confirmpassword" type="password" />
      </el-form-item>
    </el-form>
    <el-button style="float: right" @click="submitEdit">确认修改</el-button>

  </div>
</template>

<script>
// import { updatePassword } from '@/api/Login.js'
import { Message } from 'element-ui'

export default {
  name: 'Index',
  data() {
    return {
      editForm: {
        oldpassword: '',
        newpassword: '',
        confirmpassword: ''
      },
      editRule: {
        oldpassword: [{ required: true, message: '请输入原密码' }],
        newpassword: [{ required: true, message: '请输入新密码' }, { min: 6, message: '密码要求不小于6位' }],
        confirmpassword: [{ required: true, message: '请确认密码' }, { min: 6, message: '密码要求不小于6位' }]
      }
    }
  },
  methods: {
    submitEdit() {
      this.$refs['editForm'].validate((valid) => {
        if (valid) {
          if (this.editForm.newpassword == this.editForm.confirmpassword) {
            this.$store.dispatch('user/updatePassword', this.editForm).then(() => {
              this.$router.push({ path: this.redirect || '/login' })
              Message({
                type: 'success',
                message: '修改成功，请重新登录',
                duration: 3 * 1000
              })
            }).catch((e) => {
              console.log(e)
            })
          } else {
            Message.error('两次输入密码不一致！')
          }
          // updatePassword({
          //   oldpassword: this.editForm.oldpassword,
          //   newpassword: this.editForm.newpassword
          // }).then(res => {
          //   if (res.code) {
          //     Message({
          //       type: 'success',
          //       message: '修改成功',
          //       duration: 3 * 1000
          //     })
          //   }
          //   this.$route.push({ path: '/Login' })
          //   this.$store.commit('CHANGE_LOGIN_STATE', false)
          //   sessionStorage.setItem('login', false)
          // })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
